Skip to content

fix Gemma 4 MoE router -- softmax order + fuse norm dispatches - #228

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
TheTom:pr/upstream-gemma4-moe
May 14, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
TheTom:pr/upstream-gemma4-moe

Conversation

@TheTom

@TheTom TheTom commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Proposed changes

Two improvements to Gemma4TextRouter:

  1. Softmax order: softmax was applied to all 128 expert scores before top-k
    selection, so selected weights came from the full distribution rather than just
    the top-k. Moved softmax to after selection so it operates only on the chosen
    experts. Also drops the renormalization step (divide by sum) since softmax on
    top-k already produces a valid distribution.

  2. Fused norm: norm + scale used 3 separate dispatches (norm(x),
    x * rootSize, x * scale). Fused into one MLXFast.rmsNorm call
    with weight = scale * rootSize.

Results

gemma-4-26b-a4b-it-4bit on M5 Max 128GB:

Decode speed: 32.7 → 35.9 tok/s (+9.8% from fused norm)

Raw completion (no chat template, greedy):

output
before \n\nsدle- l_e_e_e_e_e
after \n\nin the ownle of kind-approachter-approachter

Garbled characters → recognizable English. Both degrade to repetition without
a chat template, but routing fix produces coherent tokens.

Long generation (500 tokens, chat template, cryptography essay):

Both produce coherent essays. The fixed router covers more historical ground
in the same token budget (includes Scytale, Al-Kindi, frequency analysis
attribution that the original version omits).

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@TheTom
TheTom marked this pull request as draft April 21, 2026 15:50
@TheTom
TheTom force-pushed the pr/upstream-gemma4-moe branch from 25d551c to 6bd3747 Compare April 21, 2026 16:09
@TheTom TheTom changed the title add Gemma 4 26B MoE support -- router, experts, dual-path forward fix Gemma 4 MoE router to match Python mlx-lm Apr 21, 2026
@TheTom
TheTom marked this pull request as ready for review April 21, 2026 16:10
@TheTom TheTom changed the title fix Gemma 4 MoE router to match Python mlx-lm fix Gemma 4 MoE router softmax order + fuse norm dispatches Apr 21, 2026
@TheTom
TheTom force-pushed the pr/upstream-gemma4-moe branch from 6bd3747 to bb856ae Compare April 21, 2026 16:13
@TheTom TheTom changed the title fix Gemma 4 MoE router softmax order + fuse norm dispatches fix Gemma 4 MoE router -- softmax order + fuse norm dispatches Apr 21, 2026
Softmax was applied to all 128 expert scores before top-k selection, so
selected weights came from the full distribution rather than just the
top-k. Moved softmax to after selection so it operates only on the chosen
experts. Drops the renormalization step since softmax on top-k already
produces a valid distribution.

Also fuses norm + scale from 3 dispatches into one MLXFast.rmsNorm call.
@TheTom
TheTom force-pushed the pr/upstream-gemma4-moe branch from bb856ae to 7ad51df Compare April 21, 2026 16:14
@TheTom

TheTom commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

before/after on gemma-4-26b-a4b-it-4bit, greedy decode (no chat template, no temperature):

prompt: "Explain the difference between TCP and UDP in one sentence:"

before (softmax all 128 experts → top-k):

\n\nsدle- l_e_e_e_e_e

after (top-k → softmax selected):

\n\nin the ownle of kind-approachter-approachter

both degrade to repetition without a chat template, but the routing fix produces recognizable English instead of garbled characters.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes look good, thank you!

@davidkoski
davidkoski merged commit 84ab278 into ml-explore:main May 14, 2026
3 checks passed
NivDvir added a commit to NivDvir/mlx-swift-lm that referenced this pull request May 27, 2026
Per davidkoski's ml-explore#238 standard ("people can always go back to the PR
the lines came from to see the discussion") applied harder: the
two comment blocks I'd kept were both restatements of what the
variable names and call structure already say.

Brings the comment-line ratio on this PR from ~40% to 0%, in line
with the maintainer's own recent PRs (ml-explore#283 2%, ml-explore#235 3%, ml-explore#228 0%).
neuromechanist added a commit to yooz-labs/mlx-swift-lm that referenced this pull request Jun 22, 2026
Bring the LLM text path to parity with the complete MLXVLM Gemma4
implementation so text-only loading of MoE (26B-A4B) and E-series KV-sharing
(E4B) checkpoints works:

- MoE block: Gemma4TextRouter + Gemma4TextExperts (SwitchGLU), dense+sparse
  decoder branch gated on enable_moe_block, and the fused-expert weight remap
  in sanitize (experts.gate_up_proj/down_proj -> switch_glu.*).
- has_kv-conditional attention: k_proj/k_norm/v_norm are optional; KV-shared
  layers (index >= firstKvSharedLayerIdx) carry none and reuse an earlier
  layer's KV at runtime.

Ported from MLXVLM/Models/Gemma4.swift (ml-explore PRs ml-explore#180, ml-explore#228); the E-series
KV-sharing parallels the in-flight ml-explore#330 / ml-explore#350. Verified via yooz-engine's
InfiniteLive parity: 26B-A4B (correct answer), E4B (exact greedy), 12B (exact
greedy), all at native context.
davidkoski pushed a commit that referenced this pull request Jul 21, 2026
Brings the MLXVLM Gemma4 MoE implementation to the LLM text path so the
26B-A4B (MoE) checkpoint loads through MLXLLM, not just MLXVLM. Adds
Gemma4TextRouter, Gemma4TextExperts (SwitchGLU), the enable_moe_block config +
dense/sparse decoder branch, and the fused-expert weight remap in sanitize.
The E-series KV-sharing it pairs with is already upstream (#342).

Ported from MLXVLM/Models/Gemma4.swift (ml-explore PRs #180, #228).
dkgv pushed a commit to dkgv/mlx-swift-lm that referenced this pull request Jul 30, 2026
…plore#228)

Softmax was applied to all 128 expert scores before top-k selection, so
selected weights came from the full distribution rather than just the
top-k. Moved softmax to after selection so it operates only on the chosen
experts. Drops the renormalization step since softmax on top-k already
produces a valid distribution.

Also fuses norm + scale from 3 dispatches into one MLXFast.rmsNorm call.
dkgv pushed a commit to dkgv/mlx-swift-lm that referenced this pull request Jul 30, 2026
Brings the MLXVLM Gemma4 MoE implementation to the LLM text path so the
26B-A4B (MoE) checkpoint loads through MLXLLM, not just MLXVLM. Adds
Gemma4TextRouter, Gemma4TextExperts (SwitchGLU), the enable_moe_block config +
dense/sparse decoder branch, and the fused-expert weight remap in sanitize.
The E-series KV-sharing it pairs with is already upstream (ml-explore#342).

Ported from MLXVLM/Models/Gemma4.swift (ml-explore PRs ml-explore#180, ml-explore#228).
davidtai added a commit to davidtai/mlx-swift-lm that referenced this pull request Oct 6, 2026
Integrate buffered and overlapping compression-window state across calls, preserving the causal pool mask and enabling the existing chunked-prefill regression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants